<?php
namespace jy_cashier;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	public function getRand($proArr)
	{
		$result = '';
		$proSum = array_sum($proArr);
		foreach ($proArr as $key => $proCur) {
			$randNum = mt_rand(1, $proSum);
			if ($randNum <= $proCur) {
				$result = $key;
				break;
			}
			$proSum -= $proCur;
		}
		unset($proArr);
		return intval($result);
	}
	public function checktofee($cashierid=0,$money=0,$mid=0){
		if(!$mid){
			return false;
		}
		$cashier=pdo_get('jy_cashier_user',array('id'=>$cashierid));
		$set=json_decode($cashier['set'],true);
		if($set['credit1'] && $set['credit1_double']){
			$fee=$money*$set['credit1_double'];
			if($fee>0 && $cashier['iskou_merch']){
				if($cashier['mid']){
					$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
					if($member['credit1']<$fee){
						exi('商户积分不足，无法送积分，收银失败！','error');
					}
				}else{
					exi('商户未绑定【收款微信号】，无法送积分，收银失败！','error');
				}
			}
		}
		return true;
	}
	public function uptofee($pay_logid=0){
		pdo_delete('jy_weishop_member_cart',array('mid'=>$_SESSION['uid']));
		$pay_log=pdo_get('jy_cashier_pay_log',array('id'=>$pay_logid));
		if(!$pay_log['mid']){
			return false;
		}
		$cashier=pdo_get('jy_cashier_user',array('id'=>$pay_log['cashierid']));
		$orderid=pdo_getcolumn('jy_cashier_order',array('id'=>$pay_log['orderid']),'orderid');
		$order=model('jy_weishop/order')->get_order($orderid);
		if($order['couponid']){
			model('jy_weishop/coupon')->usecoupon($order['couponid'],$order['ordersn']);
		}
		model('jy_commission/commission')->create($order);
		$set=json_decode($cashier['set'],true);
		if($set['credit1'] && $set['credit1_double']){
			$fee=$pay_log['money']*$set['credit1_double'];
			if($fee>0){
				if($cashier['iskou_merch']){
					if($cashier['mid']){
						$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
						if($member['credit1']<$fee){
							exi('商户积分不足，消费送积分失败！','error');
						}
						model('core/member')->member_credit($cashier['mid'],'credit1',$fee,false,'jy_cashier','商户扣积分（送客户消费积分）');
					}else{
						exi('商户未绑定【收款微信号】，消费送积分失败！','error');
					}
				}
				model('core/member')->member_credit($pay_log['mid'],'credit1',$fee,true,'jy_cashier','消费送积分');
			}
		}
		return true;
	}
	public function upfee($money,$id=0){
		$setting=$this->plugin_setting('setting');
		if($setting['is_opensale']){
			$saleset=$setting;
		}else{
			$saleset=$this->plugin_setting('jy_cashier'.$id);
		}
		$jianfee1=0;
		$jianfee2=0;
		$discount=0;
		$lastfee=$money;
		if($money>=$saleset['limit']){
			if($saleset['randtime']['tstart']<time() && $saleset['randtime']['tend']>time()){
				$temreward = array();
				foreach($saleset['probability'] as $pv){
					$temreward[] = $value['jilv'];
				}
				if($temreward){
					$reward_id = $this->model('model')->getRand($temreward);
					$jianfee1=rand($saleset['probability'][$reward_id]['start'],$saleset['probability'][$reward_id]['end']);
				}
				
			}
		}
		if($saleset['enoughtime']['tstart']<time() && $saleset['enoughtime']['tend']>time()){
				paixu($saleset['enough'],'money','desc');
				foreach($saleset['enough'] as $sv){
					if($sv['money']<=$money){
						$jianfee2=$sv['jian'];
						break;
					}
				}	
		}
		$lastfee -=$jianfee1;
		$lastfee -=$jianfee2;
		if($saleset['discounttime']['tstart']<time() && $saleset['discounttime']['tend']>time()){
				if($saleset['discount']>0){
					$lastfee=$lastfee*$saleset['discount']/100;
					$discount=$saleset['discount'];
				}
		}		
		$return=array(
			'jianfee1'=>$jianfee1,
			'jianfee2'=>$jianfee2,
			'discount'=>$discount,
			'lastfee'=>$lastfee>0?$lastfee:1,
			'money'=>$money,
		);
		return $return;
	}
}